package com.zmn.plat.admin.controller.product.tariff.tariffproductitem3;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3Query;
import com.zmn.plat.model.entity.service.item.ServiceItem;
import com.zmn.plat.services.interfaces.product.tariff.tariffproductitem3.TariffProductItem3Service;
import com.zmn.plat.services.interfaces.service.item.ServiceItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * 价格表-产品-项目-计价器3.0基础查询Controller
 *
 * @author guoqiao
 * @since 2021/11/19 15:28
 */
@Slf4j
@RestController
@RequestMapping("/product/tariff/tariffproductitem3")
public class TariffProductItem3ListController extends ProductTariffGroupItemBase3Controller {
    @Resource
    private ServiceItemService serviceItemService;
    @Resource
    private TariffProductItem3Service tariffProductItem3Service;

    /**
     * 描述: 查询列表
     *
     * @author guoqiao
     * @since 2021/11/19 15:28
     */
    @RequestMapping("/list")
    public ModelAndView list(TariffProductItem3Query query) {

        List<TariffProductItem3> items = tariffProductItem3Service.listByQuery(query);

        ModelAndView mv = new ModelAndView("product.tariff/tariffproductitem3/list");
        mv.addObject("items", items);
        mv.addObject("vo", query);

        return mv;
    }

    /**
     * 描述: 详情
     *
     * @author guoqiao
     * @since 2021/11/19 15:28
     */
    @RequestMapping("/addEditView")
    public ModelAndView addEditView(@RequestParam(value = "relateId") Integer relateId) {

        TariffProductItem3 tariffProductItem3;
        if (NumberUtil.isNullOrZero(relateId)) {
            tariffProductItem3 = new TariffProductItem3();
        } else {
            tariffProductItem3 =  tariffProductItem3Service.findById(relateId);
        }
        ModelAndView mv = new ModelAndView("product.tariff/tariffproductitem3/detail");
        mv.addObject("tariffProductItem3", tariffProductItem3);

        return mv;
    }

    @RequestMapping("/setPrice")
    public ModelAndView setPrice(@RequestParam(value = "serviceItemId", defaultValue = "0") Integer serviceItemId,
                                 @RequestParam(value = "productId", defaultValue = "0") Integer productId){
        ServiceItem serviceItem = serviceItemService.findByKey(serviceItemId);
        TariffProductItem3 item = new TariffProductItem3();
        if (serviceItem != null){
            TariffProductItem3Query query = new TariffProductItem3Query();
            query.setItemId(serviceItem.getItemId());
            query.setGroupId(serviceItem.getGroupId());
            query.setTariffId(serviceItem.getTariffId());
            query.setProductId(productId);
            List<TariffProductItem3> items = tariffProductItem3Service.listByQuery(query);
            if (items != null && items.size() > 0){
                item = items.get(0);
                /**
                 * 将单位分转换成元
                 */
                item.setHourFee(item.getHourFee() / 100);
                item.setTrafficFee(item.getTrafficFee() / 100);
                item.setPartFee(item.getPartFee() / 100);
                item.setInternalSettlementPrice(Objects.isNull(item.getInternalSettlementPrice()) ? null : item.getInternalSettlementPrice() / 100);
                item.setExternalSettlementPrice(Objects.isNull(item.getExternalSettlementPrice()) ? null : item.getExternalSettlementPrice() / 100);
            }
        }
        ModelAndView mv = new ModelAndView("product/service/group/setprice");
        mv.addObject("serviceItem",serviceItem);
        mv.addObject("item",item);
        mv.addObject("productId",productId);
        return mv;
    }

}
